package com.holly.unit.gen.api.util;

import cn.hutool.core.util.StrUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 类描述: 文件工具类
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/1/14 14:09
 */
public class FileUtil {

    private static List<String> noSelectList = new ArrayList<>();

    private static List<String> noSelectEndWithList = new ArrayList<>();

    static {
        noSelectList.add(".svn");
        noSelectList.add("CVS");
        noSelectList.add(".cvsignore");
        noSelectList.add(".copyarea.db");
        noSelectList.add("SCCS");
        noSelectList.add("vssver.scc");
        noSelectList.add(".DS_Store");
        noSelectList.add(".git");
        noSelectList.add(".gitignore");
        noSelectEndWithList.add(".ftl");
    }

    public static boolean isNoSelectFile(File file) {
        for (int i = 0; i < noSelectList.size(); i++) {
            if (file.getName().equals(noSelectList.get(i))) {
                return true;
            }
        }
        return false;

    }

    public static boolean isNoSelectEndWithFile(File file) {
        for (int i = 0; i < noSelectEndWithList.size(); i++) {
            if (file.getName().endsWith(noSelectEndWithList.get(i))) {
                return true;
            }
        }
        return false;

    }

    public static List<File> queryAllNoIgnoreFile(File dir) throws Exception{
        List<File> res = new ArrayList<>();
        queryAllNoIgnoreFile(dir,res);
        Collections.sort(res, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                return o1.getAbsolutePath().compareTo(o2.getAbsolutePath());
            }
        });
        return res;
    }

    public static void queryAllNoIgnoreFile(File dir,List<File> collec) throws Exception {

        if ((!dir.isHidden() && dir.isDirectory()) && !isNoSelectFile(dir)) {
            File[] subFiles = dir.listFiles();
            for (int i=0;i<subFiles.length;i++) {
                queryAllNoIgnoreFile(subFiles[i],collec);
            }
        } else {
            if (!isNoSelectEndWithFile(dir) && !isNoSelectFile(dir)) {
                collec.add(dir);
            }
        }

    }

    /***
     * 获取相对路径
     * @param baseDir
     * @param file
     * @return
     */
    public static String getRelativePath(File baseDir,File file) {
        if (baseDir.equals(file)) return "";
        if (baseDir.getParent() == null) return file.getAbsolutePath().substring(baseDir.getAbsolutePath().length());
        return file.getAbsolutePath().substring(baseDir.getAbsolutePath().length()+1);
    }

    /***
     * 检查文件是否是二进制文件
     * @param file
     * @return
     */
    public static boolean isBinFile(File file) {
        if (file.isDirectory()) return false;
        return isBinFile(file.getName());
    }


    public static boolean isBinFile(String fileName) {
        if (StrUtil.isBlank(getExtName(fileName))) return false;
        return true;
    }

    /***
     * 获取文件后缀名
     * @param fileName
     * @return
     */
    public static String getExtName(String fileName) {
        if (fileName == null) return "";
        int index = fileName.indexOf(".");
        if (index == -1) {
            return "";
        } else {
            return fileName.substring(index+1);
        }
    }


    public static File createParentDir(String file) {
        if (file == null) throw new IllegalArgumentException("file not null");
        File res = new File(file);
        createParentDir(res);
        return res;
    }

    public static void createParentDir(File out){
        if (out.getParentFile() != null) {
            out.getParentFile().mkdirs();
        }
    }

}
